<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\Video;
use App\Models\Message;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class VideoController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Video(), function (Grid $grid) {
            $grid->model()->whereIn('type',[2,3]);
            $grid->column('id')->sortable();
            $grid->column('sort');
            $grid->column('user_id');
            $grid->column('type')
                 ->using([2 => '视频',3 => '小视频'])
                 ->label([1 => 'primary',2 => 'success']);
            $grid->column('title');
            $grid->column('status')
                ->using([0 => '隐藏',1 => '显示'])
                ->label([0 => 'danger',1 => 'success']);
            $grid->column('audit_status')
                ->using([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                ->label([1 => 'danger',2 => 'success',3 => 'warning']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('title')->width(3);
                $filter->equal('type')
                       ->select([2 => '视频',3 => '小视频'])
                       ->width(3);
                $filter->equal('audit_status')
                    ->select([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                    ->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Video(), function (Show $show) {
            $show->field('id');
            $show->field('sort');
            $show->field('type');
            $show->field('user_id');
            $show->field('title');
            $show->field('video_url');
            $show->field('content');
            $show->field('status');
            $show->field('audit_status');
            $show->field('reject_reason');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Video(), function (Form $form) {
            $form->display('id');
            $form->number('sort')
                 ->required();
            $form->selectTable('user_id','用户')
                ->from(UserTable::make())
                ->model(User::class,'id','nickname')
                ->required();


            $form->text('title')
                ->maxLength(255)
                ->required();
            $form->radio('type')
                 ->options([2 => '视频',3 => '小视频'])
                 ->required();

            $form->multipleFile('media_url','视频')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->required();

            $form->display('media_url','浏览视频')
                ->customFormat(function ($value) {
                    $html = '';
                    foreach ($value as $v) {
                        $html .= '<video width="320" height="240" controls>
    <source src="' . $v . '" type="video/mp4">
</video>';
                        return $html;
                    }

                });


            $form->editor('content')
                 ->required();


            $form->radio('audit_status')
                ->options([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                ->when(3,function (Form $form){
                    $form->textarea('reject_reason','拒绝原因')
                        ->saving(function ($value){
                            return (string)$value;
                        })->rules('max:255',['max' => '最多500字']);
                })->required();


            $form->saved(function (Form $form,$result){
                if ($result){
                    if (!$form->input('audit_time') && $form->input('audit_status')==2){
                        DB::table('articles')
                            ->where('id',$form->getKey())
                            ->update([
                                'audit_time' => date('Y-m-d'),
                            ]);
                        if ($form->input('user_id')){
                            event(new \App\Events\Article($form->model()));
                        }

                    }

                    if ($form->input('user_id') && $form->input('audit_status')==2){
                        event(new \App\Events\Message([
                            'user_id' =>  $form->input('user_id'),
                            'type' => 2,
                            'content' => '恭喜发布的视频已通过审核'
                        ]));
                    }

                    if ($form->input('user_id') && $form->input('audit_status')==3){
                        event(new \App\Events\Message([
                            'user_id' =>  $form->input('user_id'),
                            'type' => 2,
                            'content' => '您发布的视频未通过审核'.$form->input('reject_reason')
                        ]));
                    }


                }
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
